﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Artem.Google.UI;

namespace mojoPortal.Web.RS.Controls
{
    public partial class ModalViewMapControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void LoadMap(string address, string title, string info)
        {
            
            Marker m = new Marker();
            m.Address = address;
            m.Title = title;
            m.Info = info;
            GoogleMap1.Address = address;  //this is the center of the Map coordinate.
            GoogleMap1.Markers.Add(m);
        }
        private void LoadMultipleMarkers(string address, string title, string info)
        {

        }
        public void ShowMapAllLocations(int companyid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT DISTINCT Street + ', ' + City + ', ' + State + ' ' + Zip AS Location FROM dbo.rs_tblLocation WHERE CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                    {
                        address = myDataReader["Location"].ToString();
                    }
                    //if (myDataReader["LocationID"] != DBNull.Value)
                    //{
                    //    title = "LocationID: " + myDataReader["LocationID"].ToString();
                    //}
                    //info = title + "<br/>" + address;
                    info = address;
                    if (address != "")
                    {
                        LoadMultipleMarkers(address, title, info);
                        //Marker m = new Marker();
                        //m.Address = address;
                        //m.Title = title;
                        //m.Info = info;
                        //GoogleMap1.Address = address;  //this is the center of the Map coordinate.
                        //GoogleMap1.Markers.Add(m);
                    }
                }
            }
            LoadMap("", "", "");
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByLocationID(int companyid, int locationid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT Street + ', ' + City + ', ' + State + ' ' + Zip AS Location, LocationId FROM dbo.rs_tblLocation WHERE LocationId=@LocationId AND CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@locationid";
            param.Value = locationid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                    {
                        address = myDataReader["Location"].ToString();
                    }
                    if (myDataReader["LocationID"] != DBNull.Value)
                    {
                        title = "LocationID: " + myDataReader["LocationID"].ToString();
                    }
                    info = title + "<br/>" + address;
                    if (address != "")
                        LoadMap(address, title, info);
                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByPropertyID(int companyid, int propertyid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT Street + ', ' + City + ', ' + State + ' ' + Zip AS Location, PropertyID FROM dbo.rs_tblProperty WHERE PropertyID=@propertyid AND CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@propertyid";
            param.Value = propertyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                    {
                        address = myDataReader["Location"].ToString();
                    }
                    if (myDataReader["PropertyID"] != DBNull.Value)
                    {
                        title = "PropertyID: " + myDataReader["PropertyID"].ToString();
                    }
                    info = title + "<br/>" + address;
                    if (address != "")
                        LoadMap(address, title, info);
                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByMLSID(int mlsid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT Street + ', ' + City + ', ' + State + ' ' + Zip AS Location, MLSID FROM dbo.rs_tblMLS WHERE MLSID=@mlsid";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@mlsid";
            param.Value = mlsid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                    {
                        address = myDataReader["Location"].ToString();
                    }
                    if (myDataReader["MLSID"] != DBNull.Value)
                    {
                        title = "MLSID: " + myDataReader["MLSID"].ToString();
                    }
                    info = title + "<br/>" + address;
                    if (address != "")
                        LoadMap(address, title, info);
                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByCallID(int companyid, int callid)
        {
        }
        public void ShowMapDataByWorkOrderID(int companyid, int WorkOrderid)
        {
            String assignto = "";
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT * FROM rs_vwMapWorkOrder WHERE workorderid=@WorkOrderId AND CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                        address = myDataReader["Location"].ToString();

                    title = "WorkOrder ID: " + myDataReader["workorderid"].ToString() + " - ";
                    if (myDataReader["AssignTo"] != DBNull.Value)
                    {
                        assignto = "<br/>AssignTo:<br/>" + myDataReader["AssignTo"].ToString();
                    }

                    if (myDataReader["WorkOrderName"] != DBNull.Value)
                    {
                        title = title + myDataReader["WorkOrderName"].ToString();
                    }
                    info = title;
                    if (myDataReader["TypeName"] != DBNull.Value)
                    {
                        info = info + "<br/>" + myDataReader["TypeName"].ToString();
                    }
                    info = info + "<br/>" + address;
                    info = info + assignto;
                    if (address != "")
                        LoadMap(address, title, info);

                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByEstimateID(int companyid, int Estimateid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT * FROM rs_vwMapEstimate WHERE EstimateId=@EstimateId AND CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@Estimateid";
            param.Value = Estimateid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                        address = myDataReader["Location"].ToString();

                    title = "Estimate ID: " + myDataReader["EstimateID"].ToString() + " - ";

                    if (myDataReader["WorkOrderName"] != DBNull.Value)
                    {
                        title = title + myDataReader["WorkOrderName"].ToString();
                    }
                    info = title;
                    if (myDataReader["TypeName"] != DBNull.Value)
                    {
                        info = info + "<br/>" + myDataReader["TypeName"].ToString();
                    }
                    info = info + "<br/>" + address;

                    if(address != "")
                        LoadMap(address, title, info);

                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByBillingClientID(int companyid, int Clientid)
        {
            String title = "";
            String info = "";
            String address = "";
            String strSQL = "SELECT BillingStreet + ', ' + BillingCity + ', ' + BillingState + ' ' + BillingZipCode AS Location, ClientId FROM dbo.rs_tblClient WHERE ClientId=@ClientId AND CompanyId=@CompanyId";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@Clientid";
            param.Value = Clientid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                GoogleMap1.Visible = true;
                while (myDataReader.Read())
                {
                    address = "";
                    title = "";
                    info = "";
                    if (myDataReader["Location"] != DBNull.Value)
                    {
                        address = myDataReader["Location"].ToString();
                    }

                    info = title + "<br/>" + address;
                    if (address != "")
                        LoadMap(address, title, info);
                }
            }
            myConnection.Close();
            mpeModalViewMap.Show();
        }
        public void ShowMapDataByWorkOrderStatusID(int companyid, int WorkOrderstatusid)
        {
            //String strSQL = "SELECT * FROM rs_vwLocationWithWorkOrder WHERE WorkOrderStatusId=@WorkOrderStatusId AND CompanyId=@CompanyId";
            //SqlCommand myCommand;
            //SqlParameter param;
            //SqlConnection myConnection;
            //myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            //myCommand = new SqlCommand(strSQL, myConnection);
            //SqlDataReader myDataReader;
            //param = new SqlParameter();
            //param.SqlDbType = SqlDbType.Int;
            //param.ParameterName = "@WorkOrderstatusid";
            //param.Value = WorkOrderstatusid;
            //myCommand.Parameters.Add(param);

            //param = new SqlParameter();
            //param.SqlDbType = SqlDbType.Int;
            //param.ParameterName = "@companyid";
            //param.Value = companyid;
            //myCommand.Parameters.Add(param);

            //myConnection.Open();
            //myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            //if (myDataReader.HasRows)
            //{
            //    while (myDataReader.Read())
            //    {
            //        WorkOrderid = int.Parse(myDataReader["workorderid"].ToString());
            //        MapMarker mm = new MapMarker(WorkOrderid);
            //        //if(myDataReader["Longitude"] != DBNull.Value)
            //        //    mm.Longitude = myDataReader["Longitude"].ToString();
            //        //if (myDataReader["Latitude"] != DBNull.Value)
            //        //    mm.Latitude = myDataReader["Latitude"].ToString();
            //        if (myDataReader["Location"] != DBNull.Value)
            //            mm.Address = myDataReader["Location"].ToString();
            //        if (myDataReader["WorkOrderName"] != DBNull.Value)
            //            mm.AdditionalDescription = myDataReader["WorkOrderName"].ToString();

            //        if (myDataReader["workorderid"] != DBNull.Value)
            //            mm.QuickLink = "WorkOrderID: " + myDataReader["workorderid"].ToString();
            //        if (myDataReader["WorkOrderName"] != DBNull.Value)
            //            mm.QuickLink = mm.QuickLink + " - " + myDataReader["WorkOrderName"].ToString();

            //        if (myDataReader["workorderid"] != DBNull.Value)
            //            mm.BubbleTitle = "WorkOrderID: " + myDataReader["workorderid"].ToString();
            //        if (myDataReader["WorkOrderName"] != DBNull.Value)
            //            mm.BubbleTitle = mm.BubbleTitle + " - " + myDataReader["WorkOrderName"].ToString();

            //        if (myDataReader["WorkOrderName"] != DBNull.Value)
            //            mm.BubbleDescription = myDataReader["WorkOrderName"].ToString();
            //        if (myDataReader["Location"] != DBNull.Value)
            //            mm.BubbleDescription = mm.BubbleDescription + "<br/>" + myDataReader["Location"].ToString();
            //        if (myDataReader["AssignTo"] != DBNull.Value)
            //            mm.BubbleDescription = mm.BubbleDescription + "<br/>" + myDataReader["AssignTo"].ToString();

            //        lstMarkers.Add(mm);
            //    }
            //}
            //myConnection.Close();
        }
        protected void btnModalMapOK_Click(object sender, EventArgs e)
        {
            GoogleMap1.Visible = false;
            mpeModalViewMap.Hide();
        }
        protected void btnLocationDetail_Click(object sender, EventArgs e)
        {

        }

    }
}